package com.csw.data.utils;

import com.csw.data.annotation.*;

import java.lang.reflect.Field;

/**
 * sql 生成工具类
 *
 * @Author ChenSW
 * @Date 2016/12/24 10:37
 */
public class SqlUtils {

    /**
     * 获取条件查询语句，id不考虑
     *
     * @param entity 操作对象
     * @return
     */
    public static String findByCondition(Object entity) throws Exception {

        if (entity == null)
            throw new IllegalArgumentException();

        Class<?> cls = entity.getClass();
        Field[] fields = cls.getDeclaredFields();
        Table table = cls.getAnnotation(Table.class);
        if (table == null)
            throw new RuntimeException(cls.getName() + " must have Table.class");

        if (CollectionUtils.isEmpty(fields))
            return null;


        StringBuffer sql = new StringBuffer("select * from ").append(table.value()).append(" where 1 = 1 ");
        for (Field field : fields) {

            field.setAccessible(true);

            Id id = field.getAnnotation(Id.class);
            if (id != null)
                continue;

            OneToMany oneToMany = field.getAnnotation(OneToMany.class);
            if (oneToMany != null)
                continue;

            ManyToOne manyToOne = field.getAnnotation(ManyToOne.class);
            if (manyToOne != null) {
                String relation = manyToOne.relation();

                Object obj = field.get(entity);
                if (obj == null)
                    continue;

                Field parent = obj.getClass().getDeclaredField(relation);
                if (parent == null)
                    throw new RuntimeException("ManyToOne entity not exist");

                parent.setAccessible(true);
                Object pc = parent.get(obj);
                if (!StringUtils.isEmpty(pc))
                    sql.append(" and ").append(manyToOne.column()).append(" = '").append(pc).append("' ");

                continue;
            }

            String columnName = "";
            Column column = field.getAnnotation(Column.class);
            if (column == null)
                columnName = field.getName();

            Object c = field.get(entity);
            if (!StringUtils.isEmpty(c))
                sql.append(" and ").append(columnName).append(" = '").append(c).append("' ");

        }

        return sql.toString();
    }


    /**
     * 根据主键查询
     *
     * @param entity
     * @return
     */
    public static String findById(Object entity) throws IllegalAccessException {

        if (entity == null)
            return null;

        Class<?> cls = entity.getClass();

        Field[] fields = cls.getDeclaredFields();

        Table table = cls.getAnnotation(Table.class);

        if (table == null)
            throw new RuntimeException(cls.getName() + " must have Table.class");

        StringBuffer sql = new StringBuffer("select * from ").append(table.value()).append(" where 1 = 1 ");

        if (CollectionUtils.isEmpty(fields))
            return sql.toString();

        for (Field field : fields) {
            field.setAccessible(true);
            Id id = field.getAnnotation(Id.class);
            if (id == null)
                continue;
            String column = id.value();

            column = StringUtils.isEmpty(column) ? field.getName() : column;

            Object value = field.get(entity);
            if (value == null)
                throw new NullPointerException("ID must be not null");

            sql.append(" and ").append(column).append(" = '").append(value).append("'");

        }
        return sql.toString();
    }


}
